var express = require('express');
var router = express.Router();

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

'use strict'

const should = require('should');
const path = require('path');
const {
    Configuration,
    Web3jService,
    CompileService
} = require('nodejs-sdk/packages/api');

const config = new Configuration('./conf/config.json'); //user
const contractPath = './contracts/v4/supplychain.sol';
const compileService = new CompileService(config);
const web3jService = new Web3jService(config);
const contractClass = compileService.compile(contractPath);
const Smart = contractClass.newInstance();
const supplychain_address = require('../contracts/conf.js').supplychainAddress
/* GET home page. */
router.get('/', async function (req,res) {

	const client_address = req.query.address.toString();
		console.log(client_address)
	
    let supplychain = contractClass.newInstance();
    supplychain.$load(web3jService, address);
	
	try{
		const _balance = await supplychain.getBalance(client_address)
		const _name = await supplychain.getName(client_address)
		res.json({
			errno:0,
			data:{
				balance : _balance[0],
				name : _name[0]
			}
		})
	}catch(_balance){
		res.json({
			errno:1,
			data:{}
		})
	}
	
	//const _name = await supplychain.getName('0x77610c6fa001bb97bf71ad335698b36545bc8caf')
});

module.exports = router;
